# Social Mobilisation in Partisan Spaces
# Replication Materials
# Florian Foos, Peter John, Christian Mueller, and Kevin Cunningham

# Questions to f.foos@lse.ac.uk

#install.packages("interflex", dependencies = TRUE)
#install.packages("car", dependencies = TRUE)
#library("dplyr")
#library("lmtest")
#install.packages("readstata13")
#install.packages("randomizr")
#install.packages("ri")
#install.packages("estimatr")
#install.packages("texreg")

library(interflex)
library(car)
library(dplyr)
library(lmtest)
library(readstata13)
library(randomizr)
library(ri)
library(estimatr)
library(texreg)


rm(list=ls())

options(scipen=999)

setwd("")

dataset <- as.data.frame(readRDS("data.rds"))

names(dataset)

#Recode

dataset$include<-NA
dataset$include[dataset$latest_voter_id_observed==1]<-1
dataset$include[dataset$addr_hh_size>10]<-NA
dataset$include[dataset$unable_to_vote==TRUE]<-NA

dataset$indirect<-NA
dataset$indirect[dataset$experiment_sample== "indirect"]<-1

dataset$direct<-NA
dataset$direct[dataset$experiment_sample== "direct"]<-1

dataset$party<-NA
dataset$party[dataset$latest_voter_id_conservative==1]<-1
dataset$party[dataset$latest_voter_id_labour==1]<-1
dataset$party[dataset$latest_voter_id_rivalparty==1]<-1

dataset$labour<-NA
dataset$labour[dataset$latest_voter_id_labour==1]<-1

dataset$rival<-NA
dataset$rival[dataset$latest_voter_id_conservative==1]<-1
dataset$rival[dataset$latest_voter_id_rivalparty==1]<-1

dataset$treat<-NA
dataset$treat[dataset$treatment_assign=="treated"]<-1
dataset$treat[dataset$treatment_assign=="control"]<-0

dataset$male<-NA
dataset$male[dataset$gender=="M"]<-1
dataset$male[dataset$gender=="F"]<-0
dataset$male[dataset$gender=="U"]<-0

#subset sample

spillover<-subset(dataset,!is.na(indirect))

direct<-subset(dataset,!is.na(direct))


direct_pid<-subset(direct,!is.na(include))

spillover_pid<-subset(spillover,!is.na(include))

direct_pid<-subset(direct_pid,!is.na(marked_register_2014))

direct_pid2<-subset(direct_pid,!is.na(party))

direct_labour<-subset(direct_pid,!is.na(labour))

spillover_pid<-subset(spillover_pid,!is.na(marked_register_2014))

spillover_pid<-subset(spillover_pid,!is.na(street_rival_share))

spillover_pid2<-subset(spillover_pid,!is.na(party))

spillover_pid3<-subset(spillover_pid2,!is.na(labour))

spillover_pid4<-subset(spillover_pid2,!is.na(rival))



spillover_pid_analysis2<-subset(spillover_pid2,!is.na(match_top1_all_rival))

spillover_pid_analysis3<-subset(spillover_pid3,!is.na(match_top1_all_rival))

spillover_pid_analysis4<-subset(spillover_pid4,!is.na(match_top1_all_rival))


#create IP weights

Z<-spillover_pid$treat

probs <- genprobexact(Z, blockvar=spillover_pid$ward)
probs

spillover_pid$w <- Z/probs + (1-Z)/(1-probs)


Z<-spillover_pid2$treat

probs <- genprobexact(Z, blockvar=spillover_pid2$ward)
probs

spillover_pid2$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid2$treat

probs <- genprobexact(Z, blockvar=spillover_pid2$ward)
probs

spillover_pid2$w <- Z/probs + (1-Z)/(1-probs)


Z<-spillover_pid3$treat

probs <- genprobexact(Z, blockvar=spillover_pid3$ward)
probs

spillover_pid3$w <- Z/probs + (1-Z)/(1-probs)

Z<-spillover_pid4$treat

probs <- genprobexact(Z, blockvar=spillover_pid4$ward)
probs

spillover_pid4$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis2$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis2$ward)
probs

spillover_pid_analysis2$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis3$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis3$ward)
probs

spillover_pid_analysis3$w <- Z/probs + (1-Z)/(1-probs)



Z<-spillover_pid_analysis4$treat

probs <- genprobexact(Z, blockvar=spillover_pid_analysis4$ward)
probs

spillover_pid_analysis4$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_pid$treat

probs <- genprobexact(Z, blockvar=direct_pid$ward)
probs


direct_pid$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_pid2$treat

probs <- genprobexact(Z, blockvar=direct_pid2$ward)
probs


direct_pid2$w <- Z/probs + (1-Z)/(1-probs)


Z<-direct_labour$treat

probs <- genprobexact(Z, blockvar=direct_labour$ward)
probs


direct_labour$w <- Z/probs + (1-Z)/(1-probs)


#Table 1

#Model 1

direct_effects<-lm_robust(marked_register_2014~treat+factor(ward), weights=w, clusters=street, data=direct_pid)

summary(direct_effects)

#Model 2

indirect_effects<-lm_robust(marked_register_2014~treat+factor(ward), clusters=street, weights = w, data=spillover_pid)

summary(indirect_effects)


#Table 1

table1<-texreg(list(direct_effects, indirect_effects), include.ci = FALSE,  include.rmse = FALSE, include.rsquared = FALSE, omit.coef = "ward", include.adjrs = FALSE, include.bic=FALSE, digits = 3)

table1


#Table 2


#Model I

modelI<-lm_robust(marked_register_2014~treat+factor(ward), clusters=street, weights = w, data=spillover_pid2)

summary(modelI)


#Model II

modelII<-lm_robust(marked_register_2014~treat*street_rival_share+factor(ward), clusters=street, weights = w, data=spillover_pid2)

summary(modelII)



#Model III

modelIII<-lm_robust(marked_register_2014~treat+factor(ward), clusters=street, weights = w, data=spillover_pid3)

summary(modelIII)


#Model IV
modelIV<-lm_robust(marked_register_2014~treat*street_rival_share+factor(ward), clusters=street, weights = w, data=spillover_pid3)

summary(modelIV)


#Model V

modelV<-lm_robust(marked_register_2014~treat+factor(ward), clusters=street, weights = w, data=spillover_pid4)

summary(modelV)


#Model VI
modelVI<-lm_robust(marked_register_2014~treat*street_rival_share+factor(ward), clusters=street, weights = w, data=spillover_pid4)

summary(modelVI)


#Table 2

table2<-texreg(list(modelI, modelII, modelIII, modelIV, modelV, modelVI), include.ci = FALSE,  include.rmse = FALSE, include.rsquared = FALSE, omit.coef = "ward", include.adjrs = FALSE, include.bic=FALSE, digits = 3)

table2


#Figure 2a

pdf(paste("Figure_Lab_analysis1.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share", FE=("ward"),cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid3, weights = "w")
dev.off()

#Figure 2b
pdf(paste("Figure_rival_analysis1.pdf"),w=7,h=5)
inter.binning(Y = "marked_register_2014", D = "treat", X="street_rival_share",  FE=("ward"), cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters on street",  Xdistr = "density", data = spillover_pid4, weights = "w")
dev.off()



#Household level analysis

spillover_pid_analysis2$match_top1_single <- as.numeric(spillover_pid_analysis2$match_top1_all_n_obs == 1)
spillover_pid_analysis2$match_top1_single_interact <- as.numeric(spillover_pid_analysis2$match_top1_single * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis2$match_top1_all_n_obs * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_dist_interact <- as.numeric(spillover_pid_analysis2$match_top1_all_dist * spillover_pid_analysis2$treat)
spillover_pid_analysis2$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis2$match_top1_all_rival*spillover_pid_analysis2$match_top1_all_dist * spillover_pid_analysis2$treat)

spillover_pid_analysis3$match_top1_single <- as.numeric(spillover_pid_analysis3$match_top1_all_n_obs == 1)
spillover_pid_analysis3$match_top1_single_interact <- as.numeric(spillover_pid_analysis3$match_top1_single * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis3$match_top1_all_n_obs * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_dist_interact <- as.numeric(spillover_pid_analysis3$match_top1_all_dist * spillover_pid_analysis3$treat)
spillover_pid_analysis3$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis3$match_top1_all_rival*spillover_pid_analysis3$match_top1_all_dist * spillover_pid_analysis3$treat)


Lab_analysis2<-inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival", Z = c("match_top1_all_dist", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact"),FE=("ward"), cl="street",  bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters in closest household",  cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis3, weights = "w")


spillover_pid_analysis4$match_top1_single <- as.numeric(spillover_pid_analysis4$match_top1_all_n_obs == 1)
spillover_pid_analysis4$match_top1_single_interact <- as.numeric(spillover_pid_analysis4$match_top1_single * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_nobs_interact <- as.numeric(spillover_pid_analysis4$match_top1_all_n_obs * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_dist_interact <- as.numeric(spillover_pid_analysis4$match_top1_all_dist * spillover_pid_analysis4$treat)
spillover_pid_analysis4$match_top1_dist_interact3 <- as.numeric(spillover_pid_analysis4$match_top1_all_rival*spillover_pid_analysis4$match_top1_all_dist * spillover_pid_analysis4$treat)


rival_analysis2<-inter.binning(Y = "marked_register_2014", D = "treat", X="match_top1_all_rival", Z=c("match_top1_all_dist", "match_top1_single", "match_top1_single_interact", "match_top1_all_n_obs", "match_top1_nobs_interact"),FE=("ward"), cl="street", bin.labs=FALSE, Ylabel = "turnout", Dlabel = "letter", Xlabel="share of rival party supporters in closest household",  cutoffs = c(0.01,0.99), Xdistr = "density", data = spillover_pid_analysis4, weights = "w")


# Write function ----------------------"

pdf <- function(...) invisible(NULL)
dev.off <- function(...) {
  tryCatch(
    expr = grDevices::dev.off(...),
    error = function(e) invisible(NULL)
  )
}



# Load plot helpers --------------------------------------------------

source("9_graph_helpers.R")


# Create plots -----------------------------------------------

#Figure 3a

plot_binning(
  Lab_analysis2,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis3,
                                           weights = "w")
)
ggsave("Lab_binning_plot_density.pdf", width = 5.6, height = 4, units = "in")


#Figure 3b

plot_binning(
  rival_analysis2,
  adjusted_density = inter_binning_density(D = "treat",
                                           X = "match_top1_all_rival",
                                           cutoffs = c(0.01,0.99),
                                           data = spillover_pid_analysis4,
                                           weights = "w")
)
ggsave("rival_binning_plot_density.pdf", width = 5.6, height = 4, units = "in")
